package com.voja.salonschedule.ads;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdView;
import com.voja.salonschedule.R;

public class MyAdListener extends AdListener {
	
	Context context;
	AdView  adView;
	
	public MyAdListener(Context context, AdView adView) {
		this.context = context;
		this.adView  = adView;
	}
	
	private Activity getActivity() {
		return (Activity)context;
	}

	@Override
	public void onAdFailedToLoad(int errCode) {
	    super.onAdFailedToLoad(errCode);
		
	    adView.setVisibility(View.GONE);
	}
	
	@Override
	public void onAdLoaded() {
	    super.onAdLoaded();
	    
	    // set bottom margins for Index activity in LANDSCAPE mode only
	    int orientation = getActivity().getResources().getConfiguration().orientation;
	    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
	        if (context.getClass().getCanonicalName().equals(context.getPackageName() + ".Index")) {
                int h = adView.getHeight();
                
                setRelativeLayoutMargin(R.id.leftMenu, 0, 0, h, 0);
                setRelativeLayoutMargin(R.id.rightContent, 0, 0, h, 0);
	        }
        }
	}
	
	private void setRelativeLayoutMargin(int id, int top, int right, int bottom, int left) {
        View v = getActivity().findViewById(id);
        if (v != null) {
            
            String layoutClassName = v.getLayoutParams().getClass().getName();
            if (layoutClassName.indexOf("RelativeLayout") >= 0) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)v.getLayoutParams();
                params.setMargins(left, top, right, bottom);
                v.setLayoutParams(params);
            }
        }
    }
	
}
